package com.taropan.wallpaper.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.util.DisplayMetrics;

public class Consts {
	/**首页*/
	public static final String API_HOMEPAGE = "http://124.232.163.232/wallpaper/index.json?count=10";
	/**壁纸列表*/
	public static final String API_WALLPAPER_LIST = "http://124.232.163.232/wallpaper/list.json?pi=2&pageSize=15&sortId=1&resolutionId=12&keyword=测试";
	/**壁纸分类*/
	public static final String API_WALLPAPER_CATEGORY = "http://124.232.163.232/wallpaper/sort.json";
	/**壁纸关键字*/
	public static final String API_WALLPAPER_KEYWORD = "http://124.232.163.232/wallpaper/keyword.json?count=";
	/**壁纸详细*/
	public static final String API_WALLPAPER_DETAIL = "http://124.232.163.232/wallpaper/detail.json?id=";
	/**壁纸下载*/
	public static final String API_WALLPAPER_DOWNLOAD = "http://124.232.163.232/wallpaper/detail.json?id=";
	
	
	/**
	 * 开始定位时间
	 */
	public static String locateBeginTime;

	/**
	 * 服务器时间相关
	 */
	public static String REALTIME = "realtime";
	public static String BOOTTIME = "boottime";
	public static String SHUTDOWNTIME = "shutdowntime";
	public static String SERVERTIME = "servertime";
	public static String SYSTEMCLOCKTIME = "systemclocktime";
	/**
	 * 结束定位时间
	 */
	public static String locateEndTime;
	/**
	 * 用户默认分组
	 */
	public static final String USER_TEAM = "defaultpersongroup";
	/**
	 * 移动发短信通道
	 */
	public static String yidong;
	/**
	 * 联通发短信通道
	 */
	public static String liantong;
	/**
	 * 电信发短信通道
	 */
	public static String dianxin;

	/** 验证网关的ip地址 */
	public Consts() {
		boolean isFristRead = true;
		try {
				System.out.println("Consts.................isFristRead.");
				InputStream input = this.getClass().getResourceAsStream(
						"/configs.xml");
				InputSource is = new InputSource(input);
				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp;
				sp = spf.newSAXParser();
				XMLReader xr = sp.getXMLReader();
				Configs config = new Configs();
				xr.setContentHandler(config);
				xr.parse(is);
				try {
					if (FileOperation.checkSDcard()) {
						input = this.getClass().getResourceAsStream(
								"/configs.xml");
						ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
						int ch;
						while ((ch = input.read()) != -1) {
							bytestream.write(ch);
						}
						byte imgdata[] = bytestream.toByteArray();

						FileOperation fp = new FileOperation("Config", ".xml");
						fp.initFile("configs");
						fp.deleteFile();
						fp.closeFile();

						fp = new FileOperation("Config", ".xml");
						fp.initFile("configs");
						fp.addLine(imgdata);
						fp.closeFile();
						bytestream.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	public class Configs extends DefaultHandler {
		/**
		 * 单标签解析开始
		 */
		@Override
		public void startElement(String uri, String localName, String name,
				Attributes attributes) throws SAXException {
			if (localName.equalsIgnoreCase("string")) {

				int length = attributes.getLength();
				for (int i = 0; i < length; i++) {
					String attributesName = attributes.getLocalName(i);
					String value = attributes.getValue(attributesName);
					if (attributesName.equalsIgnoreCase("name")) {

						if (value.equals("YIDONG")) {
							yidong = attributes.getValue("value");

						} else if (value.equals("LIANTONG")) {
							liantong = attributes.getValue("value");

						} else if (value.equals("DIANXIN")) {
							dianxin = attributes.getValue("value");
						} else if (value.equals("VERSIONNO")) {
							VERSIONNO = Integer.parseInt(attributes
									.getValue("value"));
						} else if (value.equals("LOCATEBEGINTIME")) {

							locateBeginTime = attributes.getValue("value");
						} else if (value.equals("LOCATEENDTIME")) {

							locateEndTime = attributes.getValue("value");
						}
					}
				}
			}
		}
	}

	/**
	 * 程序versionno
	 */
	public static int VERSIONNO = 1;
	/**
	 * 节点代码
	 */
	public static final String BUSINESS_NODECODE = "3.1";
}
